<template>
  <div class="profile_publish">
    <profile-title title="我的发布"></profile-title>
    <scroll v-if="publish.length > 0" class="content">
      <div>
        <publish-item v-for="index in pageSize"
                      :item-publish="publish[index - 1 + 5 * (currentPage - 1)]"
                      @refresh=" refresh"></publish-item>
      </div>
      <sorter :page-size="5"
              :total="publish.length"
              @currentPageChange="currentPageChange"></sorter>
    </scroll>
    <div v-else class="no_publish">你还没发布过动态，赶紧去发布吧~</div>
  </div>
</template>

<script>
  import Sorter from "components/content/sorter/Sorter";
  import Scroll from "components/content/scroll/Scroll";

  import ProfileTitle from "../ProfileTitle";
  import PublishItem from "./PublishItem";

  import {myPublish, PublishArticle} from "network/profile";

  export default {
    name: "ProfilePublish",
    components: {
      Sorter,
      Scroll,
      ProfileTitle,
      PublishItem,
    },
    data() {
      return {
        currentPage: 1,
        publish: []
      }
    },
    created() {
      myPublish(this.$store.state.userInfo.id).then(res => {
        console.log(res)
        res.data.data.map(item => {
          this.publish.push(new PublishArticle(item))
        })
      })
    },
    computed: {
      pageSize() {
        return this.currentPage < Math.ceil(this.publish.length / 5) && this.publish.length >= 5 ? 5 : this.publish.length - (this.currentPage - 1) * 5
      }
    },
    methods: {
      currentPageChange(page) {
        this.currentPage = page
      },
      refresh() {
        myPublish(this.$store.state.userInfo.id).then(res => {
          this.publish = []
          res.data.data.map(item => {
            this.publish.push(new PublishArticle(item))
          })
        })
      }
    }
  }
</script>

<style scoped>
  .profile_publish {
    position: relative;
  }

  .content {
    width: 790px;
    height: calc(100vh - 125px);
    background-color: #fff;
  }

  .no_publish {
    margin-top: 60px;
    color: #ccc;
    font-size: 18px;
    text-align: center;
  }
</style>
